﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using CRCDesktop.org.h1.crcDesktop.util;
using System.Net;

namespace CRCDesktop.org.h1.crcDesktop.comunicacao
{
    public class ClienteSocket
    {
        //system.net.sockets.tcpclient clientsocket = new system.net.sockets.tcpclient();
        //networkstream serverstream;

        //IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse("192.168.43.28"), 5050);
        IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5050);
        Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);

        String msgCliente;  
        String msgServer;
        int portaSocket;

        public ClienteSocket()
        {
           
        }

        public void conect()
        {
            try
            {
                //ClientSocket.Connect("127.0.0.1", PortaSocket);
                //ServerStream = ClientSocket.GetStream();

                this.ClientSocket.Connect(this.ipEnd);
            
            }catch(SocketException e){
                Console.WriteLine(e.StackTrace);
            }
        }

        public void start(string msg)
        {
           
        }

        public void onWrite()
        {
            try
            {
                //byte[] outStream = System.Text.Encoding.ASCII.GetBytes(this.MsgCliente);
                //ServerStream.Write(outStream, 0, outStream.Length);
                //ServerStream.Flush();
                byte[] clientData = new byte[1024];
                clientData = Encoding.ASCII.GetBytes(this.MsgCliente);
                NetworkStream ns = new NetworkStream(this.ClientSocket);
                ns.Write(clientData, 0, clientData.Length);
                ns.Flush();
            } catch (Exception ex){
                Console.WriteLine(ex.Message);
            }
        }

        public void onRead()
        {
            try
            {
                //byte[] inStream = new byte[20050];
                //ServerStream.Read(inStream, 0, (int)ClientSocket.ReceiveBufferSize);
                //string returnData = Encoding.ASCII.GetString(inStream);
                //this.MsgServer = returnData;
                NetworkStream ns = new NetworkStream(this.ClientSocket);
                byte[] serverData = new byte[1024];
                int length = ns.Read(serverData, 0, serverData.Length);
                this.MsgServer = Encoding.ASCII.GetString(serverData, 0, length);
                ns.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public Boolean isConnected()
        {
            return ClientSocket.Connected;
        }

        public String MsgCliente
        {
            get { return msgCliente; }
            set { msgCliente = value; }
        }

        public String MsgServer
        {
            get { return msgServer; }
            set { msgServer = value; }
        }

        public Socket ClientSocket
        {
            get { return clientSocket; }
            set { clientSocket = value; }
        }

        //public NetworkStream ServerStream
        //{
        //    get { return serverStream; }
        //    set { serverStream = value; }
        //}

        public int PortaSocket
        {
            get { return portaSocket; }
            set { portaSocket = value; }
        }
    }
}
